"""
DESCRIPTION
单选按钮
"""
import tkinter as tk
from windows import WidgetConfig


class RadioGroup:
    """
    单选按钮组
    """

    def __init__(self):
        self._list = []

    def appendElements(self, _RadioSingle):
        """
        控件注册
        @param _RadioSingle:
        @return:
        """

        _RadioSingle.Label.bind("<Button-1>", lambda e: _RadioSingle.Active(True))

        self._list.append(_RadioSingle)

    def GetActiveId(self):
        """
        获取按钮组中被激活的组件
        @return:
        """
        for i in self._list:
            if i.ActiveState:
                return i.GroupId
        return None

    def List(self):
        """
        获取列表
        @return:
        """
        return self._list

    def ActiveElement(self, ActiveId):
        """
        激活指定控件 并关闭其他的控件
        @param ActiveId: 激活的 控件的GroupId
        @return:
        """
        count = 0

        while count < len(self._list):
            if self._list[count].GroupId != ActiveId:
                print(ActiveId)
                self._list[count].Active(False)
            else:
                self._list[count].ActiveState = True
            print([(i.GroupId, i.ActiveState) for i in self._list])
            count += 1


class CheckGroup:
    """
    复选按钮组
    """

    def __init__(self):
        self._list = []

    def appendElements(self, _RadioSingle):
        """
        控件注册
        @param _RadioSingle:
        @return:
        """

        _RadioSingle.Label.bind("<Button-1>", lambda e: _RadioSingle.Active(not _RadioSingle.ActiveState))

        self._list.append(_RadioSingle)

    def GetActiveId(self):
        """
        获取按钮组中被激活的组件
        @return:
        """
        tmp = []
        print(f'self._list：{self._list}')
        for i in self._list:
            if i.ActiveState:
                print(i.GroupId)
                tmp.append(i.GroupId)
        return None if tmp == [] else tmp

    def List(self):
        """
        获取列表
        @return:
        """
        return self._list

    def ActiveElement(self, ActiveId):
        """
        激活指定控件 并关闭其他的控件
        @param ActiveId: 激活的 控件的GroupId
        @return:
        """
        count = 0

        while count < len(self._list):
            if self._list[count].GroupId == ActiveId:
                self._list[count].ActiveState = True
            # print([(i.GroupId,i.ActiveState) for i in self._list])
            count += 1



class Box:
    def __init__(self, unActiveBackColor, unActiveFontColor, text,
                 ActiveBackColor, ActiveFontColor, Group, GroupId, myWin,
                 HoverColor=None, DefaultActive=False, font=None):
        self.text = text
        self.unActiveBackColor, self.unActiveFontColor, self.ActiveBackColor, self.ActiveFontColor = \
            unActiveBackColor, unActiveFontColor, \
            ActiveBackColor, ActiveFontColor
        self.HoverColor = HoverColor if HoverColor is not None else unActiveBackColor
        self.Group = Group
        self.GroupId = GroupId
        self.ActiveState = DefaultActive
        self.Label = tk.Label(myWin, bg=self.unActiveBackColor, anchor='nw', fg=self.unActiveFontColor, text=self.text)
        if font == "Big":
            self.Label['font'] = WidgetConfig.util.Font(myWin).H2_En
        elif font == "SMALL":
            self.Label['font'] = WidgetConfig.util.Font(myWin).Text
        else:
            self.Label['font'] = WidgetConfig.util.Font(myWin).Default
        '''
         --------
        |默认操作|
         --------
        '''
        self.appendGroup()
        self.Active(DefaultActive)

    def getLabel(self):
        return self.Label

    def appendGroup(self):
        self.Group.appendElements(self)

    def Active(self, Boolean):
        """
        激活
        @param Boolean 激活 | 关闭
        @return:
        """
        print("点击事件")
        if Boolean:  # 激活
            self.Group.ActiveElement(self.GroupId)  # 唯一标识

            self.ActiveState = True
            self.Label['bg'] = self.ActiveBackColor
            self.Label["fg"] = self.ActiveFontColor

        else:
            self.ActiveState = False
            self.Label["bg"] = self.unActiveBackColor
            self.Label["fg"] = self.unActiveFontColor

if __name__ == '__main__':
    win = tk.Tk()

    g1 = CheckGroup()
    A = Box('red', 'white', "开启倒计时", "green", "white", g1, "Okay", win, "xx", DefaultActive=False)
    B = Box('red', 'white', "关闭倒计时", "green", "white", g1, "Over", win, "xx", DefaultActive=False)
    print(g1.List())
    myLabel = A.getLabel()
    myLabel['width'] = 30
    myLabel.pack()
    myLabel2 = A.getLabel()
    myLabel2['width'] = 30
    myLabel2.pack()
    B.getLabel().pack()
    btn = tk.Button(win, text="State", command=lambda: print(g1.GetActiveId()))
    btn.pack()
    win.mainloop()
